<template>
  <el-drawer :title="dialogTitle" v-model="dialogVisible" size="75%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      style="padding-right: 30px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="15">
          <el-row>
            <el-col :span="22">
              <el-form-item label="机构名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入机构名称" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="机构简称" prop="abbr">
                <el-input v-model="formData.abbr" placeholder="请输入机构简称" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="机构编码" prop="organCode">
                <el-input v-model="formData.organCode" placeholder="请输入机构编码" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="法人" prop="legalPerson">
                <el-input v-model="formData.legalPerson" placeholder="请输入法人" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="地址" prop="address">
                <el-input v-model="formData.address" placeholder="请输入地址" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="formData.email" placeholder="请输入邮箱" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="成立日期" prop="foundDate">
                <el-date-picker
                  v-model="formData.foundDate"
                  type="date"
                  value-format="x"
                  placeholder="选择成立日期"
                />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="域名" prop="domain">
                <el-input v-model="formData.domain" placeholder="请输入域名" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="ICP备案主体号" prop="icpMain">
                <el-input v-model="formData.icpMain" placeholder="请输入ICP备案主体号" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="ICP备案服务号" prop="icpServer">
                <el-input v-model="formData.icpServer" placeholder="请输入ICP备案服务号" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="版权" prop="copyright">
                <el-input v-model="formData.copyright" type="textarea" placeholder="请输入版权" />
              </el-form-item>
            </el-col>

            <el-col :span="22">
              <el-form-item label="简介" prop="suggest">
                <el-input v-model="formData.suggest" type="textarea" placeholder="请输入简介" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="宗旨" prop="slogan">
                <el-input v-model="formData.slogan"  type="textarea" placeholder="请输入宗旨" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="9">
          <el-row>
            <el-col :span="22">
              <el-form-item label="联系电话" prop="phone">
                <el-input v-model="formData.phone" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="等级" prop="level">
                <el-input v-model="formData.level" placeholder="请输入等级" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="性质" prop="nature">
                <el-input v-model="formData.nature" placeholder="请输入性质" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="业务" prop="business">
                <el-input v-model="formData.business" placeholder="请输入业务" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="formData.status">
                  <el-radio
                    v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                    :key="dict.value"
                    :label="dict.value"
                  >
                    {{ dict.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="logo" prop="logo">
                <UploadImg v-model="formData.logo" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="logo_text" prop="logoText">
                <UploadImg v-model="formData.logoText" />
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="二维码" prop="qrcode">
                <UploadImg v-model="formData.qrcode" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>

      </el-row>

      <el-form-item label="内容" prop="content">
        <Editor v-model="formData.content" />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { CompanyApi, CompanyVO } from '@/api/cms/company'

/** 机构信息 表单 */
defineOptions({ name: 'CompanyForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  abbr: undefined,
  logo: undefined,
  logoText: undefined,
  slogan: undefined,
  qrcode: undefined,
  phone: undefined,
  address: undefined,
  icpMain: undefined,
  icpServer: undefined,
  business: undefined,
  suggest: undefined,
  domain: undefined,
  organCode: undefined,
  legalPerson: undefined,
  level: undefined,
  nature: undefined,
  foundDate: undefined,
  email: undefined,
  copyright: undefined,
  remark: undefined,
  content: undefined,
  status: undefined,
})
const formRules = reactive({
  name: [{ required: true, message: '机构名称不能为空', trigger: 'blur' }],
  phone: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
  address: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
  nature: [{ required: true, message: '性质不能为空', trigger: 'blur' }],
  business: [{ required: true, message: '业务不能为空', trigger: 'blur' }],
  level: [{ required: true, message: '等级不能为空', trigger: 'blur' }],
  logo: [{ required: true, message: 'logo不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CompanyApi.getCompany(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CompanyVO
    if (formType.value === 'create') {
      await CompanyApi.createCompany(data)
      message.success(t('common.createSuccess'))
    } else {
      await CompanyApi.updateCompany(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    abbr: undefined,
    logo: undefined,
    logoText: undefined,
    slogan: undefined,
    qrcode: undefined,
    phone: undefined,
    address: undefined,
    icpMain: undefined,
    icpServer: undefined,
    business: undefined,
    suggest: undefined,
    domain: undefined,
    organCode: undefined,
    legalPerson: undefined,
    level: undefined,
    nature: undefined,
    foundDate: undefined,
    email: undefined,
    copyright: undefined,
    remark: undefined,
    content: undefined,
    status: Number(1),
  }
  formRef.value?.resetFields()
}
</script>
